package me.hacket.netrequestpro;

import java.util.List;

import com.google.gson.reflect.TypeToken;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import me.hacket.library.callback.RequestError;
import me.hacket.library.callback.TemplateCallback;
import me.hacket.library.request.BaseResponse;
import me.hacket.library.util.LogUtils;
import me.hacket.netrequestpro.base.NetManager;
import me.hacket.netrequestpro.bean.LogMessageRequest;
import me.hacket.netrequestpro.bean.Person;
import me.hacket.netrequestpro.bean.RegisterObj;
import me.hacket.netrequestpro.bean.RequestLogin;
import me.hacket.netrequestpro.bean.ResponseLogin;
import me.hacket.netrequestpro.bean.TempObj;
import me.hacket.netrequestpro.bean.UpgradeRequest;
import me.hacket.netrequestpro.bean.UpgradeResponse;

public class TestNetManagerAct extends Activity {

    private TextView tvResult;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_net_manager);

        tvResult = (TextView) findViewById(R.id.tv_result);
    }

    public void post(View v) {
        Person person = new Person("hacket", 24);
        NetManager.getInstance().post("test_obj.json", person, RegisterObj.class, "tag_hacket",
                new TemplateCallback<RegisterObj>() {
                    @Override
                    public void onResponseStatus(int code, String desc) {

                    }

                    @Override
                    public void onResponseError(RequestError error) {
                        LogUtils.e(error.getErrorMsg());
                    }

                    @Override
                    public void onResponseSuccess(RegisterObj registerObj) {
                        tvResult.setText(registerObj.toString());
                    }
                });
    }

    public void post_temp_json(View v) {
        //        Person person = new Person("hacket_post_temp_json", 24);
        RequestLogin login = new RequestLogin();
        NetManager.getInstance().post("test_temp.json", login, TempObj.class, "tag_json_hacket",
                new TemplateCallback<TempObj>() {
                    @Override
                    public void onResponseStatus(@BaseResponse.RESULT_CODE int code, String desc) {
                        LogUtils.i("hacket", code + ":" + desc);
                    }

                    @Override
                    public void onResponseError(RequestError error) {
                        LogUtils.e(error.getErrorMsg());
                    }

                    @Override
                    public void onResponseSuccess(TempObj registerObj) {
                        tvResult.setText(registerObj.toString());
                    }
                });
    }

    public void post_temp_array(View v) {
        Person person = new Person("hacket_post_temp_json", 24);

        TypeToken<List<TempObj>> typeToken = new TypeToken<List<TempObj>>() {
        };

        NetManager.getInstance().post("test_temp_array.json", person, typeToken, "tag_array_hacket",
                new TemplateCallback<List<TempObj>>() {
                    @Override
                    public void onResponseStatus(int code, String desc) {
                        LogUtils.i("hacket", code + ":" + desc);
                    }

                    @Override
                    public void onResponseError(RequestError error) {
                        LogUtils.e(error.getErrorMsg());
                    }

                    @Override
                    public void onResponseSuccess(List<TempObj> registerObj) {
                        tvResult.setText(registerObj.toString());
                    }
                });
    }

    public void login(View view) {

        RequestLogin requestLogin = new RequestLogin();

        NetManager.getInstance().post("user/login_sale", requestLogin, ResponseLogin.class, "tag_array_hacket",
                new TemplateCallback<ResponseLogin>() {
                    @Override
                    public void onResponseStatus(int code, String desc) {
                        LogUtils.i("hacket", code + ":" + desc);
                    }

                    @Override
                    public void onResponseError(RequestError error) {
                        LogUtils.e(error.getErrorMsg());
                    }

                    @Override
                    public void onResponseSuccess(ResponseLogin responseLogin) {
                        LogUtils.i("hacket", "response success,:" + responseLogin);
                        tvResult.setText(responseLogin.toString());
                    }
                });
    }

    public void upgrade(View v) {
        //        configuration/query_upgrade_rule
        UpgradeRequest upgradeRequest = new UpgradeRequest();
        NetManager.getInstance().post("configuration/query_upgrade_rule", upgradeRequest, UpgradeResponse.class,
                "upgrade_tag", new TemplateCallback<UpgradeResponse>() {
                    @Override
                    public void onResponseStatus(@BaseResponse.RESULT_CODE int code, String desc) {
                        LogUtils.i("hacket", code + ":" + desc);
                    }

                    @Override
                    public void onResponseError(RequestError error) {
                        LogUtils.e("hacket", error.getErrorMsg());
                    }

                    @Override
                    public void onResponseSuccess(UpgradeResponse upgradeResponse) {
                        LogUtils.i("hacket", "response success,:" + upgradeResponse);
                        tvResult.setText(upgradeResponse.toString());
                    }
                });
    }

    /**
     * ok success!
     *
     * @param v
     */
    public void log_upload(View v) {

//        LogMessageRequest request = new LogMessageRequest();
//        request.log.add(new LogMessageRequest.LogUploadLevel(LogMessageRequest.LogUploadLevel.L_ERROR,
//                "upload_log_test"));
//        request.log.add(new LogMessageRequest.LogUploadLevel(LogMessageRequest.LogUploadLevel.L_ERROR,
//                "upload_log_test222"));
//
//        NetManager.getInstance().post("android/log_report_gateway", request, LogMessageResponse.class, "log_tag",
//                new TemplateCallback<LogMessageResponse>() {
//                    @Override
//                    public void onResponseStatus(@BaseResponse.RESULT_CODE int code, String desc) {
//                        L.i("hacket", code + ":" + desc);
//                    }
//
//                    @Override
//                    public void onResponseError(RequestError error) {
//                        L.e("hacket", error.getErrorMsg());
//                    }
//
//                    @Override
//                    public void onResponseSuccess(LogMessageResponse logMessageResponse) {
//                        L.i("hacket", "response success,:" + logMessageResponse);
//                        tvResult.setText(logMessageResponse.toString());
//                    }
//                });
        NetManager.getInstance().postLogUpload(LogMessageRequest.LogUploadLevel.L_ERROR, "upload log hahha!");
    }

}
